//指标分类管理
/* eslint-disable */
import React, { Component } from 'react';
import { connect } from 'dva';
import { Table, Divider, Tag, InputNumber, Input, Select, Button, message, Icon } from 'antd';
import { DndProvider, DragSource, DropTarget } from 'react-dnd';
import styles from './Sjtb.less';
import update from 'immutability-helper';
import Filtrate from '../../components/common/Filtrate';
import MyPagination from '../../components/common/MyPagination';
import Container from '../../components/common/Container';
import MyTable from '../../components/common/MyTable';
import request from '../../utils//request';
import PublicService from "../../services/PublicService";
import AddModal from './sub/AddModal';
import config from '../../config';
const { Search } = Input;
const { Option } = Select;
let dragingIndex = -1;
const IconFont = Icon.createFromIconfontCN({
    scriptUrl: '//at.alicdn.com/t/font_1589202_7c747i8xu6h.js',
});

class Sjtb extends Component {
    state = {
        id:'',
        footerShow: true,
        modalTitle: '',
        addModal: false,
        loading: false,
        icTwoId: '',
        options: [],//指标下拉框
        dataSource: [],
        params: {},
        pageSize: 10,
        pageIndex: 1,
        total: 0,
        columns: [
            {
                children: [
                    {
                        title: '指标单位',
                        dataIndex: 'addTheDate',
                        key: 'addTheDate',
                        width: 150,
                        align: 'center',
                    },

                ],
                title: '指标名称',
            },
            {
                children: [
                    {
                        title: 't',
                        dataIndex: 'rubbish',
                        key: 'rubbish',
                        width: 150,
                        align: 'center',
                    },

                ],
                title: '垃圾进场量',
            }, {
                dataIndex: 'xbcw',
                children: [
                    {
                        title: 't',
                        dataIndex: 'enterFurnace',
                        key: 'enterFurnace',
                        width: 150,
                        align: 'center',
                    },

                ],
                title: '垃圾焚烧量',
            }, {
                children: [
                    {
                        title: 'KWh',
                        dataIndex: 'electricity',
                        key: 'electricity',
                        width: 150,
                        align: 'center',
                    },

                ],
                title: '发电量',
            }, {
                children: [
                    {
                        title: 'KWh',
                        dataIndex: 'onGridEnergy',
                        key: 'onGridEnergy',
                        width: 150,
                        align: 'center',
                    },

                ],
                title: '上网电量',
            },
            {
                children: [
                    {
                        title: 'KWh',
                        dataIndex: 'nonProduction',
                        key: 'nonProduction',
                        width: 200,
                        align: 'center',
                    },

                ],
                title: '非生产用电量',
            },
            {
                children: [
                    {
                        title: 'KWh',
                        dataIndex: 'powerStation',
                        key: 'powerStation',
                        width: 200,
                        align: 'center',
                    },

                ],
                title: '发电厂用电量',
            },
            {
                children: [
                    {
                        title: 'KWh',
                        dataIndex: 'synthesize',
                        key: 'synthesize',
                        width: 200,
                        align: 'center',
                    },

                ],
                title: '综合厂用电量',
            },
            {
                children: [
                    {
                        title: 'KWh',
                        dataIndex: 'byThePower',
                        key: 'byThePower',
                        width: 100,
                        align: 'center',
                    },

                ],
                title: '受电量',
            },
            {
                dataIndex: 'id',
                title: '操作',
                key: 'id9',
                width: 150,
                align: 'center',
                render: (text, record, index) => {
                    return (

                        <span>
                            <a src="javascript:" >查看</a>
                            {/* <Divider type="vertical" />
                            <a src="javascript:" onClick={this.onEdit.bind(this, record)}>处理</a> */}
                            {/* <Divider type="vertical" /> */}

                        </span>
                    )
                }
            }
        ],
    };

    componentDidMount() {
        this.onSubmit(1, 10)
    }

    onSubmit = (page, size) => {//查询
        const params = this.f1.getForm().getFieldsValue();
        params.page = page;
        params.rows = size;
        params.monthStr = params.monthStr.valueOf()

        if (params.kpiType == '1') {
            this.setState({
                columns: [
                    {
                        children: [
                            {
                                title: '指标单位',
                                dataIndex: 'addTheDate',
                                key: 'addTheDate',
                                width: 150,
                                align: 'center',
                            },

                        ],
                        title: '指标名称',
                    },
                    {
                        children: [
                            {
                                title: 't',
                                dataIndex: 'rubbish',
                                key: 'rubbish',
                                width: 150,
                                align: 'center',
                            },

                        ],
                        title: '垃圾进场量',
                    }, {
                        dataIndex: 'xbcw',
                        children: [
                            {
                                title: 't',
                                dataIndex: 'enterFurnace',
                                key: 'enterFurnace',
                                width: 150,
                                align: 'center',
                            },

                        ],
                        title: '垃圾焚烧量',
                    }, {
                        children: [
                            {
                                title: 'KWh',
                                dataIndex: 'electricity',
                                key: 'electricity',
                                width: 150,
                                align: 'center',
                            },

                        ],
                        title: '发电量',
                    }, {
                        children: [
                            {
                                title: 'KWh',
                                dataIndex: 'onGridEnergy',
                                key: 'onGridEnergy',
                                width: 150,
                                align: 'center',
                            },

                        ],
                        title: '上网电量',
                    },
                    {
                        children: [
                            {
                                title: 'KWh',
                                dataIndex: 'nonProduction',
                                key: 'nonProduction',
                                width: 200,
                                align: 'center',
                            },

                        ],
                        title: '非生产用电量',
                    },
                    {
                        children: [
                            {
                                title: 'KWh',
                                dataIndex: 'powerStation',
                                key: 'powerStation',
                                width: 200,
                                align: 'center',
                            },

                        ],
                        title: '发电厂用电量',
                    },
                    {
                        children: [
                            {
                                title: 'KWh',
                                dataIndex: 'synthesize',
                                key: 'synthesize',
                                width: 200,
                                align: 'center',
                            },

                        ],
                        title: '综合厂用电量',
                    },
                    {
                        children: [
                            {
                                title: 'KWh',
                                dataIndex: 'byThePower',
                                key: 'byThePower',
                                width: 100,
                                align: 'center',
                            },

                        ],
                        title: '受电量',
                    },
                    {
                        dataIndex: 'id',
                        title: '操作',
                        key: 'id9',
                        width: 150,
                        align: 'center',
                        render: (text, record, index) => {
                            return (

                                <span>
                                    <a src="javascript:" onClick={this.onPrint.bind(this, record)}>编辑</a>
                                    {/* <Divider type="vertical" />
                                    <a src="javascript:" onClick={this.onEdit.bind(this, record)}>处理</a> */}
                                    {/* <Divider type="vertical" /> */}

                                </span>
                            )
                        }
                    }
                ],
            })
        } else if (params.kpiType == '2') {
            this.setState({
                columns: [
                    {
                        children: [
                            {
                                title: '指标单位',
                                dataIndex: 'addTheDate',
                                key: 'addTheDate',
                                width: 150,
                                align: 'center',
                            },

                        ],
                        title: '指标名称',
                    },
                    {
                        children: [
                            {
                                title: 't',
                                dataIndex: 'fuelOil',
                                key: 'fuelOil',
                                width: 150,
                                align: 'center',
                            },

                        ],
                        title: '燃油使用量',
                    }, {
                        dataIndex: 'xbcw',
                        children: [
                            {
                                title: 't',
                                dataIndex: 'water',
                                key: 'water',
                                width: 150,
                                align: 'center',
                            },

                        ],
                        title: '水资源使用量',
                    }, {
                        children: [
                            {
                                title: 't',
                                dataIndex: 'caoh2',
                                key: 'caoh2',
                                width: 150,
                                align: 'center',
                            },

                        ],
                        title: '氢氧化钙使用量',
                    }, {
                        children: [
                            {
                                title: 't',
                                dataIndex: 'charcoal',
                                key: 'charcoal',
                                width: 150,
                                align: 'center',
                            },

                        ],
                        title: '活性炭使用量',
                    },
                    {
                        children: [
                            {
                                title: 't',
                                dataIndex: 'chelate',
                                key: 'chelate',
                                width: 200,
                                align: 'center',
                            },

                        ],
                        title: '蛰合剂使用量',
                    },
                    {
                        children: [
                            {
                                title: 't',
                                dataIndex: 'slag',
                                key: 'slag',
                                width: 200,
                                align: 'center',
                            },

                        ],
                        title: '炉渣产量',
                    },
                    {
                        children: [
                            {
                                title: 't',
                                dataIndex: 'flyAsh',
                                key: 'flyAsh',
                                width: 200,
                                align: 'center',
                            },

                        ],
                        title: '飞灰产量',
                    },
                    {
                        dataIndex: 'id',
                        title: '操作',
                        key: 'id9',
                        width: 150,
                        align: 'center',
                        render: (text, record, index) => {
                            return (

                                <span>
                                    <a src="javascript:" onClick={this.onPrint.bind(this, record)}>编辑</a>
                                    {/* <Divider type="vertical" />
                                    <a src="javascript:" onClick={this.onEdit.bind(this, record)}>处理</a> */}
                                    {/* <Divider type="vertical" /> */}

                                </span>
                            )
                        }
                    }
                ],
            })
        } else {
            this.setState({
                columns: [
                    {
                        children: [
                            {
                                title: '指标单位',
                                dataIndex: 'addTheDate',
                                key: 'addTheDate',
                                width: 150,
                                align: 'center',
                            },

                        ],
                        title: '指标名称',
                    },
                    {
                        children: [
                            {
                                title: 't',
                                dataIndex: 'coalCalorific',
                                key: 'coalCalorific',
                                width: 150,
                                align: 'center',
                            },

                        ],
                        title: '煤热值',
                    }, {
                        dataIndex: 'xbcw',
                        children: [
                            {
                                title: 't',
                                dataIndex: 'sulphurContent',
                                key: 'sulphurContent',
                                width: 150,
                                align: 'center',
                            },

                        ],
                        title: '污泥含硫量',
                    }, {
                        children: [
                            {
                                title: 't',
                                dataIndex: 'moistureContent',
                                key: 'moistureContent',
                                width: 150,
                                align: 'center',
                            },

                        ],
                        title: '润滑油含水率',
                    },
                    {
                        dataIndex: 'id',
                        title: '操作',
                        key: 'id9',
                        width: 150,
                        align: 'center',
                        render: (text, record, index) => {
                            return (

                                <span>
                                    <a src="javascript:" onClick={this.onPrint.bind(this, record)}>编辑</a>
                                    {/* <Divider type="vertical" />
                                    <a src="javascript:" onClick={this.onEdit.bind(this, record)}>处理</a> */}
                                    {/* <Divider type="vertical" /> */}

                                </span>
                            )
                        }
                    }
                ],
            })
        }


        this.setState({
            params
        })


        request({
            url: '/sx-client/sx/indicator/fillInFromPagingQuery',
            method: 'POST',
            data: params
        }).then(data => {
            console.log(data)
            if (data.rc === 0) {
                this.setState({
                    dataSource: data.ret.items,
                    total: data.ret.count,
                    params
                })
            } else {
                message.warning(`${data.err}`)
            }
        })

    }

    // 查看按钮
    onPrint = (record) => {
        console.log(record)

        if (this.state.params.kpiType == '1') {
            this.setState({
                moddleArr: [
                    {
                        type: 'input',
                        label: "垃圾进场量",
                        paramName: "rubbish",
                        key: "rubbish",
                        initialValue: record.rubbish,
                        rules: [{ ...config.reg.required }],
                        style: { width: 270 },
                        // span: 24
                    },
                    {
                        type: 'input',
                        label: "垃圾焚烧量",
                        paramName: "enterFurnace",
                        key: "enterFurnace",
                        initialValue: record.enterFurnace,
                        rules: [{ ...config.reg.required }],
                        style: { width: 270 },
                    },
                    {
                        type: 'input',
                        label: "发电量",
                        paramName: "electricity",
                        key: "electricity",
                        initialValue: record.electricity,
                        rules: [{ ...config.reg.required }],
                        style: { width: 270 },
                    },
                    {
                        type: 'input',
                        label: "上网电量",
                        paramName: "onGridEnergy",
                        key: "onGridEnergy",
                        initialValue: record.onGridEnergy,
                        rules: [{ ...config.reg.required }],
                        style: { width: 270 },
                    },
                    {
                        type: 'input',
                        label: "非生产用电量",
                        paramName: "nonProduction",
                        key: "nonProduction",
                        initialValue: record.nonProduction,
                        rules: [{ ...config.reg.required }],
                        style: { width: 270 },
                    },
                    {
                        type: 'input',
                        label: "发电厂用电量",
                        paramName: "powerStation",
                        key: "powerStation",
                        initialValue: record.powerStation,
                        rules: [{ ...config.reg.required }],
                        style: { width: 270 },
                    },
                    {
                        type: 'input',
                        label: "综合厂用电量",
                        paramName: "synthesize",
                        key: "synthesize",
                        initialValue: record.synthesize,
                        rules: [{ ...config.reg.required }],
                        style: { width: 270 },
                    },
                    {
                        type: 'input',
                        label: "受电量",
                        paramName: "byThePower",
                        key: "byThePower",
                        initialValue: record.byThePower,
                        rules: [{ ...config.reg.required }],
                        style: { width: 270 },
                    },

                ]
            })
        } else if (this.state.params.kpiType == '2') {
            this.setState({
                moddleArr: [
                    {
                        type: 'input',
                        label: "燃油使用量",
                        paramName: "fuelOil",
                        key: "fuelOil",
                        initialValue: record.fuelOil,
                        rules: [{ ...config.reg.required }],
                        style: { width: 270 },

                    },
                    {
                        type: 'input',
                        label: "水资源使用量",
                        paramName: "water",
                        key: "water",
                        initialValue: record.water,
                        rules: [{ ...config.reg.required }],
                        style: { width: 270 },
                    },
                    {
                        type: 'input',
                        label: "氢氧化钙使用量",
                        paramName: "caoh2",
                        key: "caoh2",
                        initialValue: record.caoh2,
                        rules: [{ ...config.reg.required }],
                        style: { width: 270 },
                    },
                    {
                        type: 'input',
                        label: "活性炭使用量",
                        paramName: "charcoal",
                        key: "charcoal",
                        initialValue: record.charcoal,
                        rules: [{ ...config.reg.required }],
                        style: { width: 270 },
                    },
                    {
                        type: 'input',
                        label: "蛰合剂使用量",
                        paramName: "chelate",
                        key: "chelate",
                        initialValue: record.chelate,
                        rules: [{ ...config.reg.required }],
                        style: { width: 270 },
                    },
                    {
                        type: 'input',
                        label: "炉渣产量",
                        paramName: "slag",
                        key: "slag",
                        initialValue: record.slag,
                        rules: [{ ...config.reg.required }],
                        style: { width: 270 },
                    },
                    {
                        type: 'input',
                        label: "飞灰产量",
                        paramName: "flyAsh",
                        key: "flyAsh",
                        initialValue: record.flyAsh,
                        rules: [{ ...config.reg.required }],
                        style: { width: 270 },
                    },

                ]
            })
        } else {
            this.setState({
                moddleArr: [
                    {
                        type: 'input',
                        label: "煤热值",
                        paramName: "coalCalorific",
                        key: "coalCalorific",
                        initialValue: record.coalCalorific,
                        rules: [{ ...config.reg.required }],
                        style: { width: 270 },

                    },
                    {
                        type: 'input',
                        label: "污泥含硫量",
                        paramName: "sulphurContent",
                        key: "sulphurContent",
                        initialValue: record.sulphurContent,
                        rules: [{ ...config.reg.required }],
                        style: { width: 270 },
                    },
                    {
                        type: 'input',
                        label: "润滑油含水率",
                        paramName: "moistureContent",
                        key: "moistureContent",
                        initialValue: record.moistureContent,
                        rules: [{ ...config.reg.required }],
                        style: { width: 270 },
                    },


                ]
            })
        }


        this.setState({
            addModal: true,
            footerShow: true,
            modalTitle: '编辑',
            id:record.id
        })





    };


    // 分页
    onChangePage = (page, size) => {//分页

        this.setState({
            pageSize: size,
            pageIndex: page,
        }, () => {
            this.setTimeAndSearch(this.state.params, page, size);
        })
    };

    onChangePage1 = (page, size) => {//改变最大条数

        this.setState({
            pageSize: size,
            pageIndex: 1
        }, () => {
            this.setTimeAndSearch(this.state.params, 1, size);
        })
    };

    setTimeAndSearch = (val, pageIndex, row) => {
        this.setState({
            spinning: true,
            pageIndex,
            pageSize: row
        })

        let params = {
            kpiType: val.kpiType,
            page: this.state.pageIndex,
            rows: this.state.pageSize,
            monthStr: val.monthStr
        }

        request({
            url: '/sx-client/sx/indicator/fillInFromPagingQuery',
            method: 'POST',
            data: params
        }).then(data => {
            console.log(data)
            if (data.rc === 0) {
                this.setState({
                    dataSource: data.ret.items,
                    total: data.ret.count,
                    params
                })
            } else {
                message.warning(`${data.err}`)
            }
        })

    };





    onEdit = (text, record, index) => {//编辑
        console.log(text, record, index)
        this.state.dataSource[index].flag = true
        this.setState({
            dataSource: this.state.dataSource
        })
    }






    // modal保存
    ModalOk = (val) => {
        val.id = this.state.id
        let params = {}
        console.log(this.state.params)
        if(this.state.params.kpiType == 1){
            params.kpiType = 1
            params.economicIndicators = val
        }else if(this.state.params.kpiType == 2){
            params.kpiType = 2
            params.matterEnergy = val
        }else{
            params.kpiType = 3
            params.assayIndex = val
        }

        request({
            url: '/sx-client/sx/indicator/fillInFromUpdate',
            method: 'POST',
            data: params
        }).then(data => {
            console.log(data)
            if (data.rc === 0) {

                message.success('修改成功!')
            } else {
                message.warning(`${data.err}`)
            }
            this.setState({
                  addModal:false 
            })
            this.onSubmit(1,10)
        })




    }

    // 取消保存
    ModalCancel = (type) => {
        let t = this;
        t.setState({
            [type]: false,
            modalBtnLoading: false
        })
    };

    //重置
    ResetForm = () => {
        this.setState({
            pageSize: 10,
            pageIndex: 1,
        })
        setTimeout(() => {
            this.onSubmit(1, 10)
        }, 500)
    }

    render() {

        const { options, dataSource, pageSize, pageIndex, total, columns, addModal, modalTitle, footerShow } = this.state

        let width = document.body.clientWidth;
        const height = document.body.clientHeight;
        let containerHeight = '75vh';
        let tableHeight = 330;
        if (width > 1367) {
            tableHeight = 630;
            containerHeight = '83vh';
        }


        const items = [
            {
                type: 'select',
                label: '指标类型',
                paramName: 'kpiType',
                options: [
                    { text: '经济指标', value: '1' },
                    { text: '物耗能耗指标', value: '2' },
                    { text: '化验指标', value: '3' },
                ],
            },
            {
                type: 'monthPicker',
                label: '时间选取',
                paramName: 'monthStr',
                initialValue: null,
            },



        ];
        return (

            <div className={styles.warp}>
                {
                    addModal &&
                    <AddModal
                        // dataTypes={dataTypes}
                        width={600}
                        visible={addModal}
                        title={modalTitle}
                        // modalBtnLoading={modalBtnLoading}
                        footerShow={footerShow}
                        onModalSave={this.ModalOk.bind(this)}
                        onCancel={this.ModalCancel.bind(this, 'addModal')}
                        // selectedChange={this.selectedChange}
                        moddleArr={this.state.moddleArr}

                    />
                }
                <Filtrate
                    items={items}
                    ref={ref => { this.f1 = ref }}
                    submit={this.onSubmit.bind(this, 1, 10)}
                    ResetForm={this.ResetForm}
                />

                <Container
                    style={{ height: containerHeight, position: "relative", overflow: 'auto' }}
                    header={true}
                >

                    <MyTable
                        columns={columns}
                        scroll={{ y: height - tableHeight }}
                        dataSource={PublicService.transformArrayData(dataSource, true, true, pageIndex, pageSize)}
                        pagination={false}
                    />
                    <MyPagination
                        showPage
                        pageSize={pageSize}
                        current={pageIndex}
                        total={total}
                        showSizeChanger
                        showQuickJumper
                        onChange={this.onChangePage.bind(this)}
                        onShowSizeChange={this.onChangePage1.bind(this)}
                    />
                </Container>

            </div>
        )
    }


}

export default connect()(Sjtb)